package cn.itcast.user.web;

import cn.itcast.user.config.PatternProperties;
import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

//@RefreshScope
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private PatternProperties patternProperties;
    @Autowired
    private HttpServletRequest request;

    @Value("${pattern.dateformat}")
    private String pattern;

    @GetMapping("/now")
    public String getNow(@RequestHeader(value = "hhxx",required = false) String hhxx,
                         @RequestHeader(value = "ty") String ty) {
        return LocalDateTime.now().format(
                DateTimeFormatter.ofPattern(patternProperties.getDateformat())
        ) + "," + hhxx + "," + ty;
    }

    @GetMapping("/prop")
    public PatternProperties getPatternProperties() {
        return this.patternProperties;
    }

    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id) {
        User user = userService.queryById(id);
        String address = user.getAddress();
        StringBuffer requestURL = request.getRequestURL();
        user.setAddress(address + ";request:" + requestURL);
        return user;
    }
}
